<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAllHotels, createHotel, updateHotel, deleteHotel } from '../../api/hotel'

const router = useRouter()

// 酒店列表数据
const hotelList = ref([])
// 加载状态
const loading = ref(false)

// 对话框显示控制
const dialogVisible = ref(false)
const dialogTitle = ref('添加酒店')
const isEdit = ref(false)

// 表单数据
const formData = reactive({
  hotelID: 0,
  name: '',
  address: '',
  starRating: 5
})

// 表单校验规则
const rules = {
  name: [
    { required: true, message: '请输入酒店名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在2到50个字符之间', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入酒店地址', trigger: 'blur' },
    { min: 5, max: 100, message: '长度在5到100个字符之间', trigger: 'blur' }
  ],
  starRating: [
    { required: true, message: '请选择星级', trigger: 'change' },
    { type: 'number', min: 1, max: 5, message: '星级在1到5之间', trigger: 'change' }
  ]
}

// 表单引用
const formRef = ref()

// 获取酒店列表
const fetchHotels = async () => {
  try {
    loading.value = true
    const res = await getAllHotels()
    hotelList.value = res.data
  } catch (error) {
    console.error('获取酒店列表失败', error)
    ElMessage.error('获取酒店列表失败')
  } finally {
    loading.value = false
  }
}

// 页面加载时获取数据
onMounted(() => {
  fetchHotels()
})

// 打开添加酒店对话框
const handleAdd = () => {
  resetForm()
  dialogTitle.value = '添加酒店'
  isEdit.value = false
  dialogVisible.value = true
}

// 打开编辑酒店对话框
const handleEdit = (row: any) => {
  resetForm()
  dialogTitle.value = '编辑酒店'
  isEdit.value = true
  Object.assign(formData, row)
  dialogVisible.value = true
}

// 重置表单
const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
  formData.hotelID = 0
  formData.name = ''
  formData.address = ''
  formData.starRating = 5
}

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      try {
        if (isEdit.value) {
          // 编辑酒店
          await updateHotel(formData.hotelID, {
            name: formData.name,
            address: formData.address,
            starRating: formData.starRating
          })
          ElMessage.success('更新酒店成功')
        } else {
          // 添加酒店
          await createHotel({
            name: formData.name,
            address: formData.address,
            starRating: formData.starRating
          })
          ElMessage.success('添加酒店成功')
        }
        dialogVisible.value = false
        fetchHotels() // 刷新列表
      } catch (error: any) {
        ElMessage.error(error.message || (isEdit.value ? '更新酒店失败' : '添加酒店失败'))
      }
    }
  })
}

// 删除酒店
const handleDelete = (row: any) => {
  ElMessageBox.confirm('确定要删除该酒店吗？此操作将不可逆', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteHotel(row.hotelID)
      ElMessage.success('删除酒店成功')
      fetchHotels() // 刷新列表
    } catch (error: any) {
      ElMessage.error(error.response?.data?.message || '删除酒店失败')
    }
  }).catch(() => {
    // 取消删除，不做任何操作
  })
}

// 查看房间
const handleViewRooms = (row: any) => {
  router.push(`/dashboard/rooms/${row.hotelID}`)
}
</script>

<template>
  <div class="hotels-container">
    <div class="page-header">
      <h2>酒店管理</h2>
      <el-button type="primary" @click="handleAdd">添加酒店</el-button>
    </div>
    <div class="hotels-list">
      <el-table 
        :data="hotelList" 
        style="width: 100%" 
        v-loading="loading"
        border
      >
        <el-table-column prop="hotelID" label="ID" width="80" />
        <el-table-column prop="name" label="酒店名称" />
        <el-table-column prop="address" label="地址" />
        <el-table-column prop="starRating" label="星级" width="100">
          <template #default="scope">
            <el-rate
              v-model="scope.row.starRating"
              disabled
              text-color="#ff9900"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleViewRooms(scope.row)">查看房间</el-button>
            <el-button type="warning" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加/编辑酒店对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="酒店名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入酒店名称"></el-input>
        </el-form-item>
        <el-form-item label="酒店地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入酒店地址"></el-input>
        </el-form-item>
        <el-form-item label="星级" prop="starRating">
          <el-rate v-model="formData.starRating" :max="5" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.hotels-container {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #303133;
}

.hotels-list {
  background-color: #fff;
  border-radius: 4px;
  padding: 20px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 